<?php

namespace App\Http\Resources;

use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/** @mixin Article */
class ArticleResource extends JsonResource
{
    /**
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        $data['id'] = $this->id;
        $data['title'] = $this->title;
        if(isset($this->content)){
            $data['content'] = $this->content;
        }
        if(isset($this->image)){
            if(!empty($this->image)){
                $data['image'] = strpos($this->image,'https') === false ? sprintf('%s/%s', aliyun_domain(), $this->image) : $this->image;
            }else{
                $data['image'] = "";
            }
        }
        if(isset($this->page_views)){
            $data['page_views'] = $this->page_views;
        }
        return $data;
    }
}
